﻿using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DRHotel_v1.HousekeepingAndStaffMngmt
{
    public partial class StaffManagement : Form
    {
        public StaffManagement()
        {
            InitializeComponent();

            //Setting up comboboxes
            cbxSearchBy.SelectedIndex = 0;
            cbxSearchBy2.SelectedIndex = 0;
            cbxStaffType2.SelectedIndex = 0;

            //Setting up radiobuttons
            rbnMale.Checked = true;
        }

        private void cbxSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy.Equals("Staff ID"))
            {
                ArrayList staffList = HousekeepingDBManager.GetStaffList();
                cbxSearch.Items.Clear();

                for (int i = 0; i < staffList.Count; i++)
                {
                    Staff staff2 = (Staff)staffList[i];
                    cbxSearch.Items.Add(staff2.staffID);
                }
            }
            else if (searchBy.Equals("Staff Name"))
            {
                ArrayList staffList = HousekeepingDBManager.GetStaffList();
                cbxSearch.Items.Clear();

                for (int i = 0; i < staffList.Count; i++)
                {
                    Staff staff2 = (Staff)staffList[i];
                    string staffName = staff2.firstName + " " + staff2.lastName;
                    cbxSearch.Items.Add(staffName);
                }
            }
            else if (searchBy.Equals("Gender"))
            {
                cbxSearch.Items.Clear();
                cbxSearch.Items.Add("Male");
                cbxSearch.Items.Add("Female");
            }
            else if (searchBy.Equals("Staff Type"))
            {
                cbxSearch.Items.Clear();
                cbxSearch.Items.Add("General Maintenance");
                cbxSearch.Items.Add("Room Maintenance");
                cbxSearch.Items.Add("Estate Maintenance");
                cbxSearch.Items.Add("Security");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string value = Convert.ToString(cbxSearch.SelectedItem);
            string type = cbxSearchBy.SelectedItem.ToString();

            StaffManagementRetrievedFeedback form = new StaffManagementRetrievedFeedback(type, value);
            form.ShowDialog();
        }

        private void btnRetrieveAll_Click(object sender, EventArgs e)
        {
            string type = "All";
            string value = "All";

            StaffManagementRetrievedFeedback form = new StaffManagementRetrievedFeedback(type, value);
            form.ShowDialog();
        }

        private void cbxSearchBy2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy2.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy.Equals("Staff ID"))
            {
                ArrayList staffList = HousekeepingDBManager.GetStaffList();
                cbxSearch2.Items.Clear();

                for (int i = 0; i < staffList.Count; i++)
                {
                    Staff staff2 = (Staff)staffList[i];
                    cbxSearch2.Items.Add(staff2.staffID);
                }
            }
            else if (searchBy.Equals("Staff Name"))
            {
                ArrayList staffList = HousekeepingDBManager.GetStaffList();
                cbxSearch2.Items.Clear();

                for (int i = 0; i < staffList.Count; i++)
                {
                    Staff staff2 = (Staff)staffList[i];
                    string staffName = staff2.firstName + " " + staff2.lastName;
                    cbxSearch2.Items.Add(staffName);
                }
            }
        }

        private void btnSearch2_Click(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy2.SelectedItem.ToString();
            //Getting the results for the user's selection of search by and input into form
            if (searchBy.Equals("Staff ID"))
            {
                int staffID = Convert.ToInt32(cbxSearch2.SelectedItem.ToString());
                Staff staff = HousekeepingDBManager.GetStaffByID(staffID);
                
                #region inputing data into form
                lblStaffID2.Text = staff.staffID.ToString();
                tbxFirstName.Text = staff.firstName.ToString();
                tbxLastName.Text = staff.lastName.ToString();
                tbxEmail.Text = staff.email.ToString();
                tbxAddress.Text = staff.address.ToString();
                dtpDob.Value = staff.dob;
                tbxNationality.Text = staff.nationality.ToString();
                tbxPhoneNo.Text = staff.phoneNo.ToString();
                tbxBankNo.Text = staff.bankNo.ToString();

                #region gender
                if (staff.gender == false)
                {
                    cbxGender.SelectedIndex = 1;
                }
                else if (staff.gender == true)
                {
                    cbxGender.SelectedIndex = 0;
                }
                #endregion

                #region staffType
                if (staff.staffType.Equals("General Maintenance"))
                {
                    cbxStaffType.SelectedIndex = 0;
                }
                else if (staff.staffType.Equals("Room Maintenance"))
                {
                    cbxStaffType.SelectedIndex = 1;
                }
                else if (staff.staffType.Equals("Estate Maintenance"))
                {
                    cbxStaffType.SelectedIndex = 2;
                }
                else if (staff.staffType.Equals("Security"))
                {
                    cbxStaffType.SelectedIndex = 3;
                }
                #endregion
                #endregion
            }
            else if (searchBy.Equals("Staff Name"))
            {
                string[] words = cbxSearch2.SelectedItem.ToString().Split(' ');
                string firstName = words[0];
                string lastName = words[1];

                Staff staff = HousekeepingDBManager.GetStaffByName(firstName, lastName);

                #region inputing data into form
                lblStaffID2.Text = staff.staffID.ToString();
                tbxFirstName.Text = staff.firstName.ToString();
                tbxLastName.Text = staff.lastName.ToString();
                tbxEmail.Text = staff.email.ToString();
                tbxAddress.Text = staff.address.ToString();
                dtpDob.Value = staff.dob;
                tbxNationality.Text = staff.nationality.ToString();
                tbxPhoneNo.Text = staff.phoneNo.ToString();
                tbxBankNo2.Text = staff.bankNo.ToString();

                #region staffType
                if (staff.staffType.ToString().Equals("General Maintenance"))
                {
                    cbxStaffType2.SelectedIndex = 0;
                }
                else  if (staff.staffType.ToString().Equals("Room Maintenance"))
                {
                    cbxStaffType2.SelectedIndex = 1;
                }
                else  if (staff.staffType.ToString().Equals("Estate Maintenance"))
                {
                    cbxStaffType2.SelectedIndex = 2;
                }
                else if (staff.staffType.ToString().Equals("Security"))
                {
                    cbxStaffType2.SelectedIndex = 3;
                }
                #endregion 

                #region gender
                if (staff.gender == false)
                {
                    cbxGender.SelectedIndex = 1;
                }
                else if (staff.gender == true)
                {
                    cbxGender.SelectedIndex = 0;
                }
                #endregion
                #endregion
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to update this staff?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion

                int staffID = Convert.ToInt32(lblStaffID2.Text);
                string firstName = tbxFirstName.Text;
                string lastName = tbxLastName.Text;
                string email = tbxEmail.Text;
                string address = tbxAddress.Text;
                DateTime dob = dtpDob.Value;
                string nationality = tbxNationality.Text;
                string phoneNo = tbxPhoneNo.Text;
                string staffType = cbxStaffType.SelectedItem.ToString();
                string bankNo = tbxBankNo.Text;

                bool gender = false;
                if (cbxGender.SelectedItem.ToString().Equals("Male"))
                {
                    gender = true;
                }
                else if (cbxGender.SelectedItem.ToString().Equals("Female"))
                {
                    gender = false;
                }

                #region check existing staff & update staff
                string name = firstName + lastName;
                ArrayList allStaff = HousekeepingDBManager.GetAllStaffList();
                bool insert = true;
                foreach (Staff staff2 in allStaff)
                {
                    string name2 = staff2.firstName + staff2.lastName;
                    if (name2.Equals(name))
                    {
                        MessageBox.Show("Staff already exists!");
                        insert = false;
                        break;
                    }
                }

                if (insert == true)
                {
                    int success = HousekeepingDBManager.UpdateStaffByID(staffID, firstName, lastName, email, address, dob, gender, nationality, phoneNo, staffType, bankNo);

                    if (success == 1)
                    {
                        MessageBox.Show("Staff updated successfully!");
                    }
                    else
                    {
                        MessageBox.Show("Staff not updated, please try again!");
                    }
                }
                #endregion 
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to create this Staff?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion

                #region declare staff
                Staff staff = new Staff();
                staff.firstName = tbxFirstName2.Text;
                staff.lastName = tbxLastName2.Text;
                staff.email = tbxEmail2.Text;
                staff.address = tbxAddress2.Text;
                staff.dob = dtpDob2.Value;
                staff.nationality = tbxNationality2.Text;
                staff.phoneNo = tbxPhoneNo2.Text;
                staff.staffType = cbxStaffType2.SelectedItem.ToString();
                staff.bankNo = tbxBankNo2.Text;

                #region staff gender
                if (rbnFemale.Checked)
                {
                    staff.gender = false;
                }
                else if (rbnMale.Checked)
                {
                    staff.gender = true;
                }
                #endregion
                #endregion

                #region check existing staff & insert staff
                //Check for existing staff
                string name = staff.firstName + staff.lastName;
                ArrayList allStaff = HousekeepingDBManager.GetAllStaffList();
                bool insert = true;
                foreach (Staff staff2 in allStaff)
                {
                    string name2 = staff2.firstName + staff2.lastName;
                    if (name2.Equals(name))
                    {
                        MessageBox.Show("Staff already exists!");
                        insert = false;
                        break;
                    }
                }

                //If staff doesnt existing, insert staff
                if (insert == true)
                {
                    bool success = HousekeepingDBManager.InsertStaff(staff);

                    if (success == true)
                    {
                        MessageBox.Show("Staff created successfully!");

                    }
                    else
                    {
                        MessageBox.Show("Staff not created, please try again!");
                    }
                }
                #endregion
            }
        }

        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // Validation of user input
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        private void tbxFirstName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxFirstName.Text))
            {
                MessageBox.Show("First name required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxFirstName.SelectAll();
                tbxFirstName.Focus();
                return;
            }
        }
        private void tbxLastName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxLastName.Text))
            {
                MessageBox.Show("Last name required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxLastName.SelectAll();
                tbxLastName.Focus();
                return;
            }
        }
        private void tbxEmail_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxEmail.Text))
            {
                MessageBox.Show("Email required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxEmail.Text, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
            {
                MessageBox.Show("Invalid Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return;
            }
        }
        private void tbxAddress_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxAddress.Text))
            {
                MessageBox.Show("Address required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxAddress.SelectAll();
                tbxAddress.Focus();
                return;
            }
        }
        private void tbxNationality_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxNationality.Text))
            {
                MessageBox.Show("Nationality required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxNationality.SelectAll();
                tbxNationality.Focus();
                return;
            }
        }
        private void tbxPhoneNo_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxPhoneNo.Text))
            {
                MessageBox.Show("Phone number required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxPhoneNo.SelectAll();
                tbxPhoneNo.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxPhoneNo.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Invalid phone number!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxPhoneNo.SelectAll();
                tbxPhoneNo.Focus();
                return;
            }
        }
        private void tbxFirstName2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxFirstName2.Text))
            {
                MessageBox.Show("First name required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxFirstName2.SelectAll();
                tbxFirstName2.Focus();
                return;
            }
        }
        private void tbxLastName2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxLastName2.Text))
            {
                MessageBox.Show("Last name required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxLastName2.SelectAll();
                tbxLastName2.Focus();
                return;
            }
        }
        private void tbxEmail2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxEmail2.Text))
            {
                MessageBox.Show("Email required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail2.SelectAll();
                tbxEmail2.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxEmail2.Text, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
            {
                MessageBox.Show("Invalid Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail2.SelectAll();
                tbxEmail2.Focus();
                return;
            }
        }
        private void tbxAddress2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxAddress2.Text))
            {
                MessageBox.Show("Address required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxAddress2.SelectAll();
                tbxAddress2.Focus();
                return;
            }
        }
        private void tbxNationality2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxNationality2.Text))
            {
                MessageBox.Show("Nationality required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxNationality2.SelectAll();
                tbxNationality2.Focus();
                return;
            }
        }
        private void tbxPhoneNo2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxPhoneNo2.Text))
            {
                MessageBox.Show("Phone number required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxPhoneNo2.SelectAll();
                tbxPhoneNo2.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxPhoneNo2.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Invalid phone number!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxPhoneNo2.SelectAll();
                tbxPhoneNo2.Focus();
                return;
            }
        }
        private void tbxBankNo_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxBankNo.Text))
            {
                MessageBox.Show("Bank number required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxBankNo.SelectAll();
                tbxBankNo.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxBankNo.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Invalid bank number!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxBankNo.SelectAll();
                tbxBankNo.Focus();
                return;
            }
        }
        private void tbxBankNo2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxBankNo2.Text))
            {
                MessageBox.Show("Bank number required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxBankNo2.SelectAll();
                tbxBankNo2.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxBankNo2.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Invalid bank number!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxBankNo2.SelectAll();
                tbxBankNo2.Focus();
                return;
            }
        }

    }
}
